/**
 * koa 主文件
 *
 * @author bian
 * @createDate 2016.8.26
 */
// library module
var koa = require("koa");
var session = require("koa-generic-session");
var koaBody = require("koa-body");
var mount = require("koa-mount");
var staticServer = require("koa-static");
var favicon = require("koa-favicon");
var koaValidate = require("koa-validate");
var io = require("socket.io");

// local module
var path = require("path");
var http = require("http");

// my module
var cache = require("./instances/cache");
var router = require("./router/index");
var log = require("./instances/log.js");

// ------------------------------------------------
var app = koa();
var pkg = require("./package.json");
var port = pkg.port;
app.env = "development";

// static file
app.use(staticServer(path.join(__dirname, "public")));
app.use(staticServer(path.join(__dirname, "views")));
// favicon
app.use(favicon(__dirname + "/public/favicon.ico"));
// http parse
app.use(koaBody());

// koa-validate app
koaValidate(app);

// route
app.use(router);

app.on("error", (err, ctx) => {
    log.error(err, ctx);
});

var server = http.createServer(app.callback());
server.listen(port);
io = io(server);
require("./socket/index.js")(io);
module.exports = server;